/*     */ package com.zimbra.qa.unittest.prov.soap;
/*     */ 
/*     */ import com.zimbra.common.mailbox.ContactConstants;
/*     */ import com.zimbra.common.soap.AccountConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.SoapTransport;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.EntrySearchFilter.Operator;
/*     */ import com.zimbra.cs.account.GalContact;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.soap.SoapProvisioning;
/*     */ import com.zimbra.cs.gal.ZimbraGalSearchBase.PredefinedSearchBase;
/*     */ import com.zimbra.qa.unittest.TestUtil;
/*     */ import com.zimbra.qa.unittest.prov.BinaryLdapData.Content;
/*     */ import com.zimbra.qa.unittest.prov.ProvTest.SkipTestReason;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestSearchGal
/*     */   extends SoapTest
/*     */ {
/*     */   private static final boolean DO_CREATE_DOMAINS = true;
/*     */   private static String DOMAIN_LDAP;
/*     */   private static String DOMAIN_GSA;
/*     */   private static String SUB1_DOMAIN_LDAP;
/*     */   private static String SUB1_DOMAIN_GSA;
/*     */   private static String SUB2_DOMAIN_LDAP;
/*     */   private static String SUB2_DOMAIN_GSA;
/*     */   private static final String AUTHED_USER = "user1";
/*     */   private static final String KEY_FOR_SEARCH_BY_NAME = "account";
/*     */   private static final String ACCOUNT_PREFIX = "account";
/*     */   private static final String DISTRIBUTION_LIST_PREFIX = "dl";
/*     */   private static final String DYNAMIC_GROUP_PREFIX = "dyngroup";
/*     */   private static final String DEPARTMENT_PREFIX = "engineering";
/*     */   private static final String BINARY_LDAP_ATTR = "userSMIMECertificate";
/*  65 */   private static final String BINARY_GALCONTACT_FIELD = ContactConstants.A_userSMIMECertificate;
/*     */   private static final int ACCOUNT_CONTAINS_BINARY_DATA = 5;
/*     */   private static final int NUM_BYTES_IN_BINARY_DATA = 100;
/*  68 */   private static final BinaryLdapData.Content BINARY_CONTENT_1 = BinaryLdapData.Content.generateContent(100);
/*     */   
/*  70 */   private static final BinaryLdapData.Content BINARY_CONTENT_2 = BinaryLdapData.Content.generateContent(100);
/*     */   
/*     */   private static final int NUM_ACCOUNTS = 10;
/*     */   
/*     */   private static final int NUM_DISTRIBUTION_LISTS = 10;
/*     */   private static final int NUM_DYNAMIC_GROUPS = 10;
/*     */   private static final int NUM_ALL_OBJECTS = 30;
/*     */   private static SoapProvTestUtil provUtil;
/*     */   private static Provisioning prov;
/*     */   
/*     */   @BeforeClass
/*     */   public static void init()
/*     */     throws Exception
/*     */   {
/*  84 */     provUtil = new SoapProvTestUtil();
/*  85 */     prov = provUtil.getProv();
/*     */     
/*  87 */     String BASE_DOMAIN_NAME = baseDomainName();
/*     */     
/*     */ 
/*  90 */     DOMAIN_LDAP = "ldap." + BASE_DOMAIN_NAME;
/*  91 */     SUB1_DOMAIN_LDAP = "sub1." + DOMAIN_LDAP;
/*  92 */     SUB2_DOMAIN_LDAP = "sub2." + SUB1_DOMAIN_LDAP;
/*     */     
/*     */ 
/*  95 */     DOMAIN_GSA = "gsa." + BASE_DOMAIN_NAME;
/*  96 */     SUB1_DOMAIN_GSA = "sub1." + DOMAIN_GSA;
/*  97 */     SUB2_DOMAIN_GSA = "sub2." + SUB1_DOMAIN_GSA;
/*     */     
/*     */ 
/* 100 */     createDomainObjects(DOMAIN_LDAP, null);
/* 101 */     createDomainObjects(SUB1_DOMAIN_LDAP, ZimbraGalSearchBase.PredefinedSearchBase.SUBDOMAINS);
/* 102 */     createDomainObjects(SUB2_DOMAIN_LDAP, ZimbraGalSearchBase.PredefinedSearchBase.DOMAIN);
/*     */     
/* 104 */     createDomainObjects(DOMAIN_GSA, null);
/* 105 */     createDomainObjects(SUB1_DOMAIN_GSA, ZimbraGalSearchBase.PredefinedSearchBase.SUBDOMAINS);
/* 106 */     createDomainObjects(SUB2_DOMAIN_GSA, ZimbraGalSearchBase.PredefinedSearchBase.DOMAIN);
/*     */   }
/*     */   
/*     */   @AfterClass
/*     */   public static void cleanup() throws Exception
/*     */   {
/* 112 */     Cleanup.deleteAll(new String[] { baseDomainName() });
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void searchWithDot(boolean ldap, String domainName, int expectedNumEntries)
/*     */     throws Exception
/*     */   {
/* 129 */     SoapTransport transport = authUser(TestUtil.getAddress("user1", domainName));
/*     */     
/* 131 */     Element request = Element.create(transport.getRequestProtocol(), AccountConstants.SEARCH_GAL_REQUEST);
/* 132 */     request.addElement("name").setText(".");
/*     */     
/* 134 */     Element response = transport.invoke(request);
/*     */     
/* 136 */     boolean paginationSupported = response.getAttributeBool("paginationSupported");
/*     */     
/* 138 */     List<GalContact> result = new ArrayList();
/* 139 */     for (Element e : response.listElements("cn")) {
/* 140 */       result.add(new GalContact("id", SoapProvisioning.getAttrs(e)));
/*     */     }
/*     */     
/* 143 */     if (ldap)
/*     */     {
/* 145 */       Assert.assertFalse(paginationSupported);
/*     */     }
/*     */     else {
/* 148 */       Assert.assertTrue(paginationSupported);
/*     */     }
/*     */     
/*     */ 
/* 152 */     Assert.assertEquals(expectedNumEntries, result.size());
/*     */   }
/*     */   
/*     */   private void searchWithOffsetLimit(boolean ldap, String domainName) throws Exception {
/* 156 */     SoapTransport transport = authUser(TestUtil.getAddress("user1", domainName));
/*     */     
/* 158 */     Element request = Element.create(transport.getRequestProtocol(), AccountConstants.SEARCH_GAL_REQUEST);
/* 159 */     request.addElement("name").setText(".");
/*     */     
/* 161 */     int offset = 5;
/* 162 */     int limit = 3;
/* 163 */     request.addAttribute("offset", offset);
/* 164 */     request.addAttribute("limit", limit);
/* 165 */     request.addAttribute("sortBy", "nameAsc");
/*     */     
/* 167 */     Element response = transport.invoke(request);
/*     */     
/* 169 */     boolean paginationSupported = response.getAttributeBool("paginationSupported");
/*     */     
/* 171 */     List<GalContact> result = new ArrayList();
/* 172 */     for (Element e : response.listElements("cn")) {
/* 173 */       result.add(new GalContact("id", SoapProvisioning.getAttrs(e)));
/*     */     }
/*     */     
/* 176 */     if (ldap)
/*     */     {
/* 178 */       Assert.assertFalse(paginationSupported);
/*     */       
/*     */ 
/*     */ 
/* 182 */       Assert.assertEquals(31L, result.size());
/*     */     }
/*     */     else {
/* 185 */       Assert.assertTrue(paginationSupported);
/*     */       
/* 187 */       Assert.assertEquals(limit, result.size());
/* 188 */       for (int i = 0; i < limit; i++) {
/* 189 */         Assert.assertEquals(getAcctEmail(offset + i, domainName), ((GalContact)result.get(i)).getSingleAttr(ContactConstants.A_email));
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void searchByName(boolean ldap, String domainName) throws Exception
/*     */   {
/* 196 */     SoapTransport transport = authUser(TestUtil.getAddress("user1", domainName));
/*     */     
/* 198 */     Element request = Element.create(transport.getRequestProtocol(), AccountConstants.SEARCH_GAL_REQUEST);
/* 199 */     request.addElement("name").setText("account");
/*     */     
/* 201 */     Element response = transport.invoke(request);
/*     */     
/* 203 */     List<GalContact> result = new ArrayList();
/* 204 */     for (Element e : response.listElements("cn")) {
/* 205 */       result.add(new GalContact("id", SoapProvisioning.getAttrs(e)));
/*     */     }
/*     */     
/* 208 */     Assert.assertEquals(10L, result.size());
/*     */   }
/*     */   
/*     */   private void searchByFilter(boolean ldap, String domainName) throws Exception {
/* 212 */     SoapTransport transport = authUser(TestUtil.getAddress("user1", domainName));
/*     */     
/* 214 */     Element request = Element.create(transport.getRequestProtocol(), AccountConstants.SEARCH_GAL_REQUEST);
/* 215 */     request.addElement("name").setText(".");
/*     */     
/*     */ 
/* 218 */     Element eSearchFilter = request.addElement("searchFilter");
/* 219 */     Element eConds = eSearchFilter.addElement("conds");
/*     */     
/* 221 */     int acctToMatch = 8;
/* 222 */     Element eCondResType = eConds.addElement("cond");
/* 223 */     eCondResType.addAttribute("attr", ContactConstants.A_email);
/* 224 */     eCondResType.addAttribute("op", EntrySearchFilter.Operator.has.name());
/* 225 */     eCondResType.addAttribute("value", acctToMatch);
/*     */     
/* 227 */     String matchDepartment = getDepartment(acctToMatch, domainName);
/* 228 */     Element eCondResSite = eConds.addElement("cond");
/* 229 */     eCondResSite.addAttribute("attr", ContactConstants.A_department);
/* 230 */     eCondResSite.addAttribute("op", EntrySearchFilter.Operator.has.name());
/* 231 */     eCondResSite.addAttribute("value", matchDepartment);
/*     */     
/* 233 */     Element response = transport.invoke(request);
/*     */     
/* 235 */     List<GalContact> result = new ArrayList();
/* 236 */     for (Element e : response.listElements("cn")) {
/* 237 */       result.add(new GalContact("id", SoapProvisioning.getAttrs(e)));
/*     */     }
/*     */     
/* 240 */     Assert.assertEquals(1L, result.size());
/* 241 */     Assert.assertEquals(getAcctEmail(acctToMatch, domainName), ((GalContact)result.get(0)).getSingleAttr(ContactConstants.A_email));
/*     */   }
/*     */   
/*     */   private void binaryDataInEntry(boolean ldap, String domainName, boolean wantSMIMECert) throws Exception {
/* 245 */     SoapTransport transport = authUser(TestUtil.getAddress("user1", domainName));
/*     */     
/* 247 */     Element request = Element.create(transport.getRequestProtocol(), AccountConstants.SEARCH_GAL_REQUEST);
/* 248 */     if (wantSMIMECert) {
/* 249 */       request.addAttribute("needSMIMECerts", true);
/*     */     }
/* 251 */     request.addElement("name").setText(getAcctEmail(5, domainName));
/*     */     
/* 253 */     Element response = transport.invoke(request);
/*     */     
/* 255 */     List<GalContact> result = new ArrayList();
/* 256 */     for (Element e : response.listElements("cn")) {
/* 257 */       result.add(new GalContact("id", SoapProvisioning.getAttrs(e)));
/*     */     }
/*     */     
/* 260 */     Assert.assertEquals(1L, result.size());
/* 261 */     GalContact galContact = (GalContact)result.get(0);
/* 262 */     Map<String, Object> fields = galContact.getAttrs();
/* 263 */     Object value = fields.get(BINARY_GALCONTACT_FIELD);
/*     */     
/* 265 */     if (!wantSMIMECert) {
/* 266 */       Assert.assertNull(value);
/* 267 */       return;
/*     */     }
/*     */     
/* 270 */     Assert.assertTrue(value instanceof String[]);
/* 271 */     String[] values = (String[])value;
/* 272 */     Assert.assertEquals(2L, values.length);
/*     */     
/* 274 */     boolean foundContent1 = false;
/* 275 */     boolean foundContent2 = false;
/* 276 */     for (String valueAsString : values) {
/* 277 */       if (BINARY_CONTENT_1.equals(valueAsString)) {
/* 278 */         foundContent1 = true;
/*     */       }
/*     */       
/* 281 */       if (BINARY_CONTENT_2.equals(valueAsString)) {
/* 282 */         foundContent2 = true;
/*     */       }
/*     */     }
/*     */     
/* 286 */     Assert.assertTrue(foundContent1);
/* 287 */     Assert.assertTrue(foundContent2);
/*     */   }
/*     */   
/*     */   private static String getAcctEmail(int index, String domainName) {
/* 291 */     return TestUtil.getAddress("account-" + index, domainName);
/*     */   }
/*     */   
/*     */   private static String getDistributionListEmail(int index, String domainName) {
/* 295 */     return TestUtil.getAddress("dl-" + index, domainName);
/*     */   }
/*     */   
/*     */   private static String getDynamicGroupEmail(int index, String domainName) {
/* 299 */     return TestUtil.getAddress("dyngroup-" + index, domainName);
/*     */   }
/*     */   
/*     */   private static String getDepartment(int index, String domainName) {
/* 303 */     return TestUtil.getAddress("engineering-" + index, domainName);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private static void createDomainObjects(String domainName, ZimbraGalSearchBase.PredefinedSearchBase searchBase)
/*     */     throws Exception
/*     */   {
/* 311 */     ZimbraLog.test.info("Creating domain " + domainName);
/* 312 */     Map<String, Object> attrs = null;
/* 313 */     if (searchBase != null) {
/* 314 */       attrs = new HashMap();
/* 315 */       attrs.put("zimbraGalInternalSearchBase", searchBase.name());
/*     */     }
/* 317 */     prov.createDomain(domainName, attrs);
/*     */     
/* 319 */     prov.createAccount(TestUtil.getAddress("user1", domainName), "test123", null);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 325 */     for (int i = 0; i < 10; i++) {
/* 326 */       String acctName = getAcctEmail(i, domainName);
/* 327 */       attrs = new HashMap();
/* 328 */       attrs.put("ou", getDepartment(i, domainName));
/*     */       
/* 330 */       if (5 == i) {
/* 331 */         StringUtil.addToMultiMap(attrs, "userSMIMECertificate", BINARY_CONTENT_1.getString());
/* 332 */         StringUtil.addToMultiMap(attrs, "userSMIMECertificate", BINARY_CONTENT_2.getString());
/*     */       }
/* 334 */       prov.createAccount(acctName, "test123", attrs);
/*     */     }
/*     */     
/* 337 */     String acctName = TestUtil.getAddress("account-hide-in-gal", domainName);
/* 338 */     attrs = new HashMap();
/* 339 */     attrs.put("zimbraHideInGal", "TRUE");
/* 340 */     prov.createAccount(acctName, "test123", attrs);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 346 */     for (int i = 0; i < 10; i++) {
/* 347 */       String dlName = getDistributionListEmail(i, domainName);
/* 348 */       prov.createGroup(dlName, null, false);
/*     */     }
/*     */     
/* 351 */     String dlName = TestUtil.getAddress("dl-hide-in-gal", domainName);
/* 352 */     attrs = new HashMap();
/* 353 */     attrs.put("zimbraHideInGal", "TRUE");
/* 354 */     prov.createGroup(dlName, attrs, false);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 360 */     for (int i = 0; i < 10; i++) {
/* 361 */       String dynGroupName = getDynamicGroupEmail(i, domainName);
/* 362 */       prov.createGroup(dynGroupName, null, true);
/*     */     }
/*     */     
/* 365 */     String dynGroupName = TestUtil.getAddress("dyngroup-hide-in-gal", domainName);
/* 366 */     attrs = new HashMap();
/* 367 */     attrs.put("zimbraHideInGal", "TRUE");
/* 368 */     prov.createGroup(dynGroupName, attrs, true);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void GSASerarhWithDot() throws Exception
/*     */   {
/* 374 */     SKIP_FOR_INMEM_LDAP_SERVER(ProvTest.SkipTestReason.DN_SUBTREE_MATCH_FILTER);
/*     */     
/* 376 */     GalTestUtil.enableGalSyncAccount(prov, DOMAIN_GSA);
/*     */     
/* 378 */     searchWithDot(false, DOMAIN_GSA, 31);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void GSASerarhWithDotSubDomain() throws Exception {
/* 383 */     SKIP_FOR_INMEM_LDAP_SERVER(ProvTest.SkipTestReason.DN_SUBTREE_MATCH_FILTER);
/*     */     
/* 385 */     GalTestUtil.enableGalSyncAccount(prov, SUB1_DOMAIN_GSA);
/*     */     
/* 387 */     searchWithDot(false, SUB1_DOMAIN_GSA, 62);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void GSASerarhWithOffsetlimit() throws Exception {
/* 392 */     SKIP_FOR_INMEM_LDAP_SERVER(ProvTest.SkipTestReason.DN_SUBTREE_MATCH_FILTER);
/*     */     
/* 394 */     GalTestUtil.enableGalSyncAccount(prov, DOMAIN_GSA);
/* 395 */     searchWithOffsetLimit(false, DOMAIN_GSA);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void GSASerarhByName() throws Exception {
/* 400 */     SKIP_FOR_INMEM_LDAP_SERVER(ProvTest.SkipTestReason.DN_SUBTREE_MATCH_FILTER);
/*     */     
/* 402 */     GalTestUtil.enableGalSyncAccount(prov, DOMAIN_GSA);
/* 403 */     searchByName(false, DOMAIN_GSA);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void GSASerarhByFilter() throws Exception {
/* 408 */     SKIP_FOR_INMEM_LDAP_SERVER(ProvTest.SkipTestReason.DN_SUBTREE_MATCH_FILTER);
/*     */     
/* 410 */     GalTestUtil.enableGalSyncAccount(prov, DOMAIN_GSA);
/* 411 */     searchByFilter(false, DOMAIN_GSA);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void GSASerarhEntryWithBinaryData() throws Exception {
/* 416 */     SKIP_FOR_INMEM_LDAP_SERVER(ProvTest.SkipTestReason.DN_SUBTREE_MATCH_FILTER);
/*     */     
/* 418 */     GalTestUtil.enableGalSyncAccount(prov, DOMAIN_GSA);
/* 419 */     binaryDataInEntry(false, DOMAIN_GSA, true);
/* 420 */     binaryDataInEntry(false, DOMAIN_GSA, false);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void fallbackToLDAPSerarhWithDot() throws Exception {
/* 425 */     SKIP_FOR_INMEM_LDAP_SERVER(ProvTest.SkipTestReason.DN_SUBTREE_MATCH_FILTER);
/*     */     
/* 427 */     searchWithDot(true, DOMAIN_LDAP, 31);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void fallbackToLDAPSerarhWithDotSubDomain() throws Exception {
/* 432 */     SKIP_FOR_INMEM_LDAP_SERVER(ProvTest.SkipTestReason.DN_SUBTREE_MATCH_FILTER);
/*     */     
/*     */ 
/* 435 */     searchWithDot(true, SUB1_DOMAIN_LDAP, 62);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void fallbackToLDAPSerarhWithOffsetlimit() throws Exception {
/* 440 */     SKIP_FOR_INMEM_LDAP_SERVER(ProvTest.SkipTestReason.DN_SUBTREE_MATCH_FILTER);
/*     */     
/* 442 */     searchWithOffsetLimit(true, DOMAIN_LDAP);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void fallbackToLDAPSerarhByName() throws Exception {
/* 447 */     SKIP_FOR_INMEM_LDAP_SERVER(ProvTest.SkipTestReason.DN_SUBTREE_MATCH_FILTER);
/*     */     
/* 449 */     searchByName(true, DOMAIN_LDAP);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void fallbackToLDAPSerarhByFilter() throws Exception {
/* 454 */     SKIP_FOR_INMEM_LDAP_SERVER(ProvTest.SkipTestReason.DN_SUBTREE_MATCH_FILTER);
/*     */     
/* 456 */     searchByFilter(true, DOMAIN_LDAP);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void fallbackToLDAPSerarhEntryWithBinaryData() throws Exception {
/* 461 */     SKIP_FOR_INMEM_LDAP_SERVER(ProvTest.SkipTestReason.DN_SUBTREE_MATCH_FILTER);
/*     */     
/* 463 */     binaryDataInEntry(true, DOMAIN_LDAP, true);
/* 464 */     binaryDataInEntry(true, DOMAIN_LDAP, false);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/soap/TestSearchGal.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */